/*****************************************************************************
* File Name: trackpad_gesture_mapping.c
*
* Version: 1.0
*
* Description:
* This file contains functions for mapping gestures with associated actions (keycode, mcode, hwheel, zwheel)
*
* Note:
* Define DISABLE_TRACKPAD in platform.h to disable trackpad module
*
* Owner: SKUV
*
* Related Document:
* CapSense Gesture component datasheet
*
* Hardware Dependency:
* CapSense Gesture component
*
* Code Tested With:
* 1. PSoC Creator 3.1 
* 2. ARM GCC 4.8.4
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its roducts for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#include "trackpad.h"
#include "debug.h"
#include "trackpad_custom_gesture.h"
#include "simple_motion.h"

#if !defined(DISABLE_TRACKPAD) && !defined(TOUCH_TUNER)

extern uint32 numberOfFingers;
extern bool customGestureEnabled;
extern bool prevClickGesture;
extern CSD_Touchpad_POSITION_STRUCT fingerPosition[2];
extern uint32 prevnumberofFingers;

/*****************************************************************************
* Function Name:  Trackpad_Gesture_Map() 
******************************************************************************
* Summary:
* This function maps gesture with appropriate action (key code, modifier key, hwheel, zwheel)
*
* Parameters:
* Report *report - Pointer for report structure
* uint32 gesturecode - gesture code variable
*
* Return:
* None
*
* Theory:
* Maps the gesture to the corresponding gesture code
*
* Side Effects:
* None
*
* Note:
* None
*****************************************************************************/
ReportType Trackpad_Gesture_Map(Report *report, uint32 gesturecode) 
{
    ReportType reportType = NO_REPORT;
    bool is_pointer_movement = false;
    if(prevClickGesture)
    {
        reportType |= MOUSE_REPORT;
        prevClickGesture = false;
    }
    if(gesturecode)
      Debug_Print(DEBUG_MESSAGE_LEVEL_3, "map gesture_code = %d\r\n",gesturecode);
    switch(gesturecode)
    {
#if 1
        case 6:
            reportType |= KEYBOARD_REPORT;
            report->keyboard_report.keycode[report->keyboard_report.keylength_used++] = 0;          
            break;  
        case GESTURE_LEFT:
            reportType |= KEYBOARD_REPORT;
            report->keyboard_report.keycode[report->keyboard_report.keylength_used++] = 0x50;          
            break;
        case GESTURE_RIGHT:
            reportType |= KEYBOARD_REPORT;
            report->keyboard_report.keycode[report->keyboard_report.keylength_used++] = 0x4F;             
            break; 
        case GESTURE_UP:
            reportType |= KEYBOARD_REPORT;
            report->keyboard_report.keycode[report->keyboard_report.keylength_used++] = 0x52;  
            break;  
        case GESTURE_DOWN:
            reportType |= KEYBOARD_REPORT;
            report->keyboard_report.keycode[report->keyboard_report.keylength_used++] = 0x51;             
            break;  
        case TAP:
            reportType |= KEYBOARD_REPORT;
            report->keyboard_report.keycode[report->keyboard_report.keylength_used++] = 0x58;               
            break;  
#endif
        case CSD_Touchpad_ST_CLICK:
        case CSD_Touchpad_ST_DOUBLECLICK:
            report->mouse_report.click |= LEFTCLICK;
            reportType |= MOUSE_REPORT;
            prevClickGesture = true;
            break;
        case CSD_Touchpad_DT_CLICK:
            report->mouse_report.click |= RIGHTCLICK; 
            reportType |= MOUSE_REPORT;
            prevClickGesture = true;
            break;
        case CSD_Touchpad_EDGE_SWIPE_TOP:
            report->keyboard_report.mkey |= WINDOWS_KEY;
            reportType |= KEYBOARD_REPORT;
            break;
        case CSD_Touchpad_ZOOM_IN:         
            report->keyboard_report.mkey = CTRL_KEY;
            report->mouse_report.zwheel = SCROLLUP;
            reportType |= (KEYBOARD_REPORT | MOUSE_REPORT);
            break;
        case CSD_Touchpad_ZOOM_OUT:
            report->keyboard_report.mkey = CTRL_KEY;
            report->mouse_report.zwheel = SCROLLDOWN;
            reportType |= (KEYBOARD_REPORT | MOUSE_REPORT); 
            break;
        case EDGE_VERTICAL_SCROLL_NORTH:
#ifndef DISABLE_CUSTOM_SCROLL_GESTURE
            report->mouse_report.zwheel = Trackpad_Custom_Scroll_Count(); 
            reportType |= MOUSE_REPORT;
#endif /* DISABLE_CUSTOM_SCROLL_GESTURE */
            break;
        case EDGE_VERTICAL_SCROLL_SOUTH:
#ifndef DISABLE_CUSTOM_SCROLL_GESTURE
            report->mouse_report.zwheel = -Trackpad_Custom_Scroll_Count();
            reportType |= MOUSE_REPORT;
#endif /* DISABLE_CUSTOM_SCROLL_GESTURE */
            break;
        case EDGE_HORIZONTAL_SCROLL_EAST:
#ifndef DISABLE_CUSTOM_SCROLL_GESTURE
            report->mouse_report.hwheel = Trackpad_Custom_Scroll_Count();
            reportType |= MOUSE_REPORT;
#endif /* DISABLE_CUSTOM_SCROLL_GESTURE */
            break;
        case EDGE_HORIZONTAL_SCROLL_WEST:
#ifndef DISABLE_CUSTOM_SCROLL_GESTURE
            report->mouse_report.hwheel = -Trackpad_Custom_Scroll_Count();
            reportType |= MOUSE_REPORT;
#endif /* DISABLE_CUSTOM_SCROLL_GESTURE */
            break;
        case DUMMY_GESTURE:
            break;
        default:
            if (numberOfFingers == 1)
            {
                /* Pointer Movement */
                reportType |= Trackpad_Custom_PointerMovement_Calulation(report, &is_pointer_movement);
            }
            break;
    }

#ifdef DIFF_POINTER_MOVEMENT
    if(!is_pointer_movement)
    {
        Trackpad_Custom_PointerMovement_Reset();
    }
#endif /* DIFF_POINTER_MOVEMENT */
    return reportType;
}

#endif /* !defined(DISABLE_TRACKPAD) && !defined(TOUCH_TUNER) */
